//
//  ByUISettingVC.m
//  laserdevice_ios
//
//  Created by pro on 16/11/21.
//  Copyright © 2016年 com.itboye. All rights reserved.
//

#import "ByUISettingVC.h"

// view
#import "ByUISettingSelectView.h"       // 选择视图

@interface ByUISettingVC () <UITableViewDelegate, UITableViewDataSource>
{
    NSArray *_languages;                // 语言数组
    NSArray *_units;                    // 单位数组
    
    NSMutableArray *_tagsForLanguage;   // 语言 选中记录
    NSMutableArray *_tagsForUnits;      // 单位 选中记录

}
@property (nonatomic, strong) UITableView *table;
@end

@implementation ByUISettingVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = STRING(@"shezhi");
    
    [self addLeftBackBarButtonItem];    // 返回按钮
    
    [self setupLocalData];              // 设置本地数据
    [self createTableView];             // 创建表视图
    
}

// 设置数据
- (void)setupLocalData {
    
    _languages  = @[@"中文（简体）", @"English（United States）"];
    _units      = @[STRING(@"mi"),
                    STRING(@"yingchi"),
                    STRING(@"yingcun"),
                    STRING(@"yingchi/yingcun"),];
    
    NSInteger selectLanguage = 0;
    if ([ByLanguageController shareInstance].mode == ByLanguageModeChinaese) {
        selectLanguage = 0;
    } else {
        selectLanguage = 1;
    }
    _tagsForLanguage = [NSMutableArray array];
    for (int i = 0; i < _languages.count; i ++) {
        if (i == selectLanguage) {
            [_tagsForLanguage addObject:@YES];
        } else {
            [_tagsForLanguage addObject:@NO];
        }
    }

    _tagsForUnits = [NSMutableArray array];
    int unit = [ByCommonCache getSystemUnit].intValue;
    for (int i = 0; i < _units.count; i ++) {
        if (i == unit) {
            [_tagsForUnits addObject:@YES];
        } else {
            [_tagsForUnits addObject:@NO];
        }
    }
    
}

// 创建表视图
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    self.table.backgroundColor = TABLE_COLOR;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    self.table.tableFooterView = [UIView new];
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数（2组，语言、单位长度）
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return _languages.count;
    } else {
        return _units.count;
    }
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"setting_cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"setting_cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        ByUISettingSelectView *selectView = [[ByUISettingSelectView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        [cell.contentView addSubview:selectView];
        selectView.tag = 1;
    }
    
    // 设置数据
    ByUISettingSelectView *selectView = (ByUISettingSelectView *)[cell.contentView viewWithTag:1];
    if (indexPath.section == 0) {
        selectView.contentLabel.text = _languages[indexPath.row];
        selectView.selected = [_tagsForLanguage[indexPath.row] boolValue];
    } else {
        selectView.contentLabel.text = _units[indexPath.row];
        selectView.selected = [_tagsForUnits[indexPath.row] boolValue];
    }
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    // 头视图高度
    CGFloat height = [self tableView:tableView heightForHeaderInSection:section];

    // 基底视图
    UIView *basalView = [[UIView alloc] init];
    basalView.backgroundColor = [UIColor whiteColor];

    // 图片
    CGFloat side = 25;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (height - side) / 2, side, side)];
    [basalView addSubview:imageView];
    
    // 文字
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(imageView.right + 10, 0, 100, height)];
    label.font = FONT(14);
    label.textColor = TEXT_COLOR;
    label.textAlignment = NSTextAlignmentLeft;
    [basalView addSubview:label];

    if (section == 0) {
        imageView.image = [UIImage imageNamed:@"setting_language"];
        label.text = STRING(@"yuyan");
    } else {
        imageView.image = [UIImage imageNamed:@"setting_unit"];
        label.text = STRING(@"changdudanwei");
    }
    
    return basalView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 点击了语言
    if (indexPath.section == 0) {
        
        for (int i = 0; i < _tagsForLanguage.count; i ++) {
            if (indexPath.row == i) {
                // 当前点击的已经是选中的，则不做操作
                if ([_tagsForLanguage[i] boolValue]) {
                    return;
                } else {
                    [_tagsForLanguage replaceObjectAtIndex:i withObject:@YES];
                    ByLanguageMode mode = (i == 0) ? ByLanguageModeChinaese : ByLanguageModeEnglish;
                    // 缓存语言
                    [ByCommonCache setSystemLanguage:mode];
                    // 设置语言
                    [[ByLanguageController shareInstance] setMode:mode];
                    // 切换窗口
                    [[ByLanguageController shareInstance] changeLanguage];
                }
            } else {
                [_tagsForLanguage replaceObjectAtIndex:i withObject:@NO];
            }
        }
    }
    // 点击了长度单位
    else {
        
        for (int i = 0; i < _tagsForUnits.count; i ++) {
            if (indexPath.row == i) {
                // 当前点击的已经是选中的，则不做操作
                if ([_tagsForUnits[i] boolValue]) {
                    return;
                } else {
                    // 缓存语言
                    [ByCommonCache setSystemUnit:i];

                    [_tagsForUnits replaceObjectAtIndex:i withObject:@YES];
                }
            } else {
                [_tagsForUnits replaceObjectAtIndex:i withObject:@NO];
            }
        }
    }
    
    // 刷新tableView
    [tableView reloadData];
}


@end
